import request from '@/utils/request'
import { randomId } from '@/utils'
import memCache from '@/utils/memCache'

// 获取购物车列表
export function getShoppingCart(params) {
  const data = new FormData()
  data.append('page', params.page || 1)
  data.append('rows', params.rows || 20)
  data.append('sort', 'createTime')
  data.append('order', 'desc')
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/page`,
    data
  })
}

// 获取购物车数量
export const getShoppingCartCount = async() => {
  const data = new FormData()
  data.append('onlyCountFlag', true)
  data.append('pageFlag', false)
  const res = await request({
    method: 'POST',
    url: `/app/v1/shoppingCart/page`,
    data,
    loading: false
  })
  return (res && res.data && res.data.total) || 0
}

/**
 * 批量查询购物车
 * @param {Array} ids 购物车ID
*/
export function queryBatchList(ids) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/queryBatch`,
    data: ids,
    loading: false
  })
}

/**
 * 加入购物车
 * @param {Object} 待加入购物车的商品数据
 {
  skuId:'',
  spuId:'',
  skuCode:'SKU编码',
  skuName:'sku名称',
  mainPicUrl:'商品主图',
  orangePrice:'原价',
  salePrice:'销售单价',
  quantity:'数量',
  remark:'产品备注',
  productFlag:'产品标识(01:否 02:样品 03:赠品 04:非常规异型 05:样品赠品)'，
  isActivity:'是否特价-活动价(1/0)',
  discount:'折扣值',
  disCode:'经销商编码',
  userId:'用户ID',
  consumerId:'顾客ID'
 }
 **/
export function addShoppingCart(data) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/add`,
    data,
    loading: true,
    message: '正在加入'
  })
}

/**
 * 删除购物车商品
 *@param {Array} ids 购物车ID集合
 *  */
export function deleteShoppingCart(ids) {
  return request({
    method: 'DELETE',
    url: `/app/v1/shoppingCart/delete`,
    data: ids,
    loading: true,
    message: '正在删除'
  })
}

/**
 * 编辑商品销售单价
 * @param {Number} salePrice 销售单价
 * @param {String} shoppingCartId 购物车ID
 * */
export function modifySalePrice({ salePrice, shoppingCartId }) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/modify`,
    data: { salePrice, shoppingCartId },
    loading: false
  })
}

/**
 * 编辑商品数量
 * @param {Number} quantity 数量
 * @param {String} shoppingCartId 购物车ID
 * */
export function modifyQuantity({ quantity, shoppingCartId }) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/modify`,
    data: { quantity, shoppingCartId },
    loading: false
  })
}

/**
 * 批量折扣
 * @param {Number} discount 折扣
 * @param {Array} ids (待折扣商品id)  // 购物车ID集合
 * */
export function modifyDiscount({ discount, ids }) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/discount`,
    data: { discount, ids },
    loading: true,
    message: '正在保存'
  })
}

/**
 * 添加备注
 * @param {Number} discount 折扣
 * @param {String} shoppingCartId 购物车ID
 * */
export function modifyRemark({ remark, shoppingCartId }) {
  return request({
    method: 'POST',
    url: `/app/v1/shoppingCart/remark`,
    data: { remark, shoppingCartId },
    loading: true,
    message: '正在保存'
  })
}

/**
 * 提交购物车
 * */
export function commitCart() {
  return request({
    method: 'get',
    url: `/app/v1/shoppingCart/commit`,
    loading: true,
    message: '正在结算'
  })
}
